<%--
  Created by IntelliJ IDEA.
  User: dd
  Date: 2023/5/4
  Time: 11:27
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>执行计划</title>
  <script type="application/javascript" src="${pageContext.request.contextPath}/static/js/jquery-3.6.1.min.js"></script>
  <link rel="stylesheet" href="${pageContext.request.contextPath}/static/layui-v2.7.6/layui/css/layui.css" >
  <script src="${pageContext.request.contextPath}/static/layui-v2.7.6/layui/layui.js"></script>
    <script src="${pageContext.request.contextPath}/static/js/qrcode.js"></script>
    <style>
        body{padding: 6px 16px;}
        .demo-carousel{height: 200px; line-height: 200px; text-align: center;}
    </style>
    <script type="text/html" id="barDemo">
        <a lay-event="edit" class="layui-btn layui-btn-xs">编辑</a>
        {{#  if(d.status==0){ }}
        <a lay-event="del" class="layui-btn layui-btn-danger layui-btn-xs">删除</a>
        {{#  } }}
        {{#  if(d.status==1){ }}
        <a lay-event="del" class="layui-btn layui-btn-normal layui-btn-xs">启用</a>
        {{#  } }}
        <a lay-event="show" class="layui-btn layui-btn-danger layui-btn-xs">分享二维码</a>
    </script>
    <script type="text/html" id="toolbarDemo">
        <div class="layui-btn-container">
            <button class="layui-btn layui-btn-sm" lay-event="getInsert">添加计划</button>

        </div>
    </script>
</head>
<body>
<form class="layui-form" action="layui">
    <div class="layui-inline">
        <label class="layui-form-label">计划名称</label>
        <div class="layui-input-inline">
            <input type="text" name="planname" placeholder="请输入计划名称" autocomplete="off" class="layui-input">
        </div>
    </div>

    <div class="layui-inline">
        <div class="layui-input-inline">
            <button class="layui-btn" lay-submit lay-filter="formDemo">查询</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>


<table id="demo" class="demo-carousel" lay-filter="test" ></table>

<form class="layui-form" action="" id="updateTestForm" style="display: none" lay-filter="updateTestFormFilter">
    <input type="hidden" name="id">
    <input type="hidden" name="uid">
    <div class="layui-form-item">
        <label class="layui-form-label">计划名称</label>
        <div class="layui-input-block">
            <input type="text" name="planname" required lay-verify="required" placeholder="请输入计划名称" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">邀请码</label>
        <div class="layui-input-block">
            <input type="text" name="code" lay-verify="required|codeExist" lay-reqtext="邀请码不能为空" placeholder="请输入邀请码" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">开始时间</label>
        <div class="layui-input-block">
            <input type="date" name="begintime" required lay-verify="required" placeholder="开始时间" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">结束时间</label>
        <div class="layui-input-block">
            <input type="date" name="endtime" required lay-verify="required" placeholder="结束时间" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">创建时间</label>
        <div class="layui-input-block">
            <input type="date" name="createtime" required lay-verify="required" placeholder="创建时间" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">修改时间</label>
        <div class="layui-input-block">
            <input type="date" name="updatetime" required lay-verify="required" placeholder="修改时间" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">状态</label>
        <div class="layui-input-block">
            <input type="radio" name="status" value="0" title="正常" checked>
            <input type="radio" name="status" value="1" title="禁用">
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="formUpdateTest">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>
<%--<div id='qrcode'></div>--%>
<script>
    $('#qrcode').qrcode({
        render: "canvas",
        width:100,
        height:100,
        text: "https://www.bilibili.com/"
    })
    layui.use(['table','form'], function(){
        let table = layui.table;
        let form = layui.form;
        table.render({
            elem: '#demo'
            ,url: '${pageContext.request.contextPath}/TSdsPlanServlet?method=query' //数据接口
            ,page: true //开启分页
            ,limit:10
            ,toolbar:'#toolbarDemo'
            // ,autoSort:false
            ,limits:[5,10,15,20,25,30,100,200]
            ,cols: [[ //表头
                {type: 'checkbox', fixed: 'left',id: 'test'},
                {field: 'id',  width:'5%',title: 'ID', sort: true, fixed: 'left' }
                ,{field: 'planname', title: '计划名称'}
                ,{field: 'begintime', title: '开始时间'}
                ,{field: 'endtime', title: '结束时间'}
                ,{field: 'code', title: '邀请码'}
                ,{field: 'uid',title: '用户id'}
                ,{field: 'createtime',title: '创建时间'}
                ,{field: 'updatetime',title: '更新时间'}
                ,{field: 'status', title: '状态' ,templet: function(d){
                        let status=d.status;
                        if (+status===0){
                            return "正常"
                        }else {
                            return "禁用"
                        }
                        //得到当前行数据，并拼接成自定义模板
                        // return 'ID：'+ d.id +'，标题：<span style="color: #c00;">'+ d.title +'</span>'
                    }}
                ,{field: 'right',width:'20%', title: '操作',toolbar:'#barDemo'}
            ]]
        });
        //判断表单中的值是否符合条件

        table.on('toolbar(test)', function(obj) {
            var checkStatus = table.checkStatus('test')
                , data = checkStatus.data; //获取选中的数据
            form.verify({
                //将用户名是否可用作为验证条件 表单提交时触发
                codeExist:function(value){
                    var params = {'code': value};
                    var message;
                    $.ajax({
                        url: "${pageContext.request.contextPath}/TSdsPlanServlet?method=check",
                        // contentType: "application/json",
                        // dataType:'json',
                        type: "post",
                        async: false,
                        // data: JSON.stringify(params),
                        data: params,
                        success: function(d){
                            if(+d.code===1){
                                message ="邀请码已存在，请重新输入！"
                            }else{

                            }
                        },

                    });
                    if (message!=null) {
                        return '邀请码已存在';
                    }

                }
            });
            switch(obj.event){
                case 'getInsert':
                    layer.open({
                        title: '添加题目',
                        type: 1,
                        content: $('#updateTestForm'), //这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它0的相对元素所影响
                        area: ['500px', '300px']
                    });
                    form.val('updateTestFormFilter', {id:"",uid:"${user.id}",planname:"",updatetime:"",createtime:"",
                    begintime:"",endtime:"",status:0,code:""});
//弹出对话框，添加用户的form表单
//监听form提交事件
                    form.on('submit(formUpdateTest)', function(index){
//ajax方式提交表单数据，将数据保存至数据库中
                        $.get(
                            "${pageContext.request.contextPath}/TSdsPlanServlet?method=add"
                            , index.field
                            ,function (d){
                                if (+d.code===0){
                                    layer.closeAll();
                                }else {
                                    layer.closeAll();
                                    layer.confirm('存在相同题目无法添加', function(){layer.closeAll();})
                                }
                            }
                            ,"json"
                        )
                        layer.close(index);
                        table.reload('demo');
                        // table.reload('demo');
                        return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
                    });
                    break;
            }
        });


        form.on('submit(formDemo)',function (data) {
            table.reload('demo', {
                url: "${pageContext.request.contextPath}/TSdsPlanServlet",
                where: { //请求参数（注意：这里面的参数可任意定义，并非下面固定的格式）
                    planname: data.field.planname //排序字段
                }
            });
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        })
        table.on('tool(test)', function(obj){ //注：tool 是工具条事件名，id 是 table 原始容器的属性 lay-filter="对应的值"
            var data = obj.data //获得当前行数据
            if(obj.event === 'del'){
                layer.confirm('是否执行？', function(index){
                    if (data.status===1){
                        data.status=0;
                    }else {
                        data.status=1;
                    }
                    $.get(
                        "${pageContext.request.contextPath}/TSdsPlanServlet?method=upd"
                        , data
                        ,function (d){
                            if (+d.code===0){
                                table.reload('demo');
                            }
                        }
                        ,"json"
                    )
                    layer.close(index);
//向服务端发送删除指令
                });

            } else if(obj.event === 'edit'){
                layer.open({
                    title: '修改题目',
                    offset:'50px',
                    type: 1,
                    content: $('#updateTestForm'), //这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
                    area: ['500px', '300px']
                });
//表单数据的填充
                form.val('updateTestFormFilter', data);
//弹出对话框，添加用户的form表单
//监听form提交事件

                form.on('submit(formUpdateTest)', function(index){

//ajax方式提交表单数据，将数据保存至数据库中
                    $.get(
                        "${pageContext.request.contextPath}/TSdsPlanServlet?method=upd"
                        , index.field
                        ,function (d){
                            if (+d.code===0){
                                table.reload('test');
                            }
                        }
                        ,"json"
                    )
                    layer.close(index);
                    return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
                });

            }else if(obj.event === 'show'){
                var qrCode="<div style='margin-left:15%;' id=\"qrcode\"></div>\n"
                layer.open({
                    type: 1, // page 层类型
                    area: ['300px', '300px'],
                    title: '二维码',
                    shade: 0.6, // 遮罩透明度
                    shadeClose: true, // 点击遮罩区域，关闭弹层
                    maxmin: true, // 允许全屏最小化
                    anim: 0, // 0-6 的动画形式，-1 不开启
                    content:qrCode,
                    success:function () {
                        $('#qrcode').qrcode({
                            render: "canvas",
                            width:200,
                            height:200,
                            foreground: "green" ,
                            text: "http://192.168.0.155:8080/w_j_h_war_exploded/viwes/jx/login2.jsp"
                        })
                    }
                });


            }
        });
    })

</script>
</body>
</html>
